<?php

namespace app\admin\controller;

use app\common\BaseHelper as Helper;
use app\common\Common;
use app\admin\model\Povertyproject;
use app\admin\model\Image;

use think\Request;

class Povertyprojectdetail extends Base
{
    public function _initialize() {
        parent::_initialize();

        $projectId = $this->request->param('projectId');
        $defaultWhere = $this->getDefaultWhere();
        $defaultWhere['projectId'] = $projectId;
        $this->defaultWhere = $defaultWhere;
        $this->searchFields = [
            'title' => [
                'label'     => '标题',
                'field'     => 'title',
                'type'      => 'text',
                'disabled'  => false,
                'condition' => 'like'
            ],
        ];

        $povertyprojectInfo = Povertyproject::where(['id' => $projectId])->find();
        $this->assign('projectId', $projectId);
        $this->assign('projectName', $povertyprojectInfo['title']);
        $this->assign('area', $this->getArea($povertyprojectInfo));
        $this->assign('povertyprojectInfo', $povertyprojectInfo);
    }

    public function index(){

        return parent::index();
    }

    public function add(Request $request) {

        return parent::add($request);
    }

    public function addPost(Request $request, $redirect = '') {
        $redirect = url('admin/povertyprojectdetail/index', ['projectId' => $request->param('projectId')]);

        return parent::addPost($request, $redirect);
    }

    public function edit(Request $request) {
        $model = model($this->model);
        $id = $request->param('id');
        // Get data info
        $info = $model->find($id);

        if(!$info){
            return $this->error('error !', 'admin/' . strtolower($this->model) . '/index');
        }

        $imgList = Image::where(['relatedId' => $info['id'], 'tag' => 'imglist'])->order('sorting ASC')->select();
        $this->assign('imgList', $imgList);
        $this->assign('imgcount', count($imgList));

        return $this->view->fetch('edit', [
            'info' => $info
        ]);
    }

    public function editPost(Request $request, $redirect = ''){
        $redirect = url('admin/povertyprojectdetail/index', ['projectId' => $request->param('projectId')]);

        return parent::editPost($request, $redirect);
    }

    public function delete() {
        return parent::delete();
    }

    /**
     * 处理列表数据
     */
    public function handleListData($data){
        foreach ($data as $item) {
            // 图集
            $imgList = Image::where(['relatedId' => $item['id'], 'tag' => 'imglist'])->order('sorting ASC')->select();
            $item->imgList = $imgList;
        }

        return $data;
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request){
        $param = $request->param();
        $where = [];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);

            if(isset($filter['donateName']) && $filter['donateName']){
                $where['donateName'] = ['like', '%'.$filter['donateName'].'%'];
            }
        }

        return $where;
    }
}
